﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Gift.DAL;
using Gift.Model;

namespace WebSite.admin.product
{
    public partial class deliveryTemplate : BaseAdmin
    {
        readonly DALDeliveryCost theDeliveryCost = new DALDeliveryCost();
        readonly DALDeliveryTemplate theDeliveryTemplate = new DALDeliveryTemplate();
        tbDeliveryTemplate oDeliveryTemplate;
        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.BindDeliveryCostList();
            }
        }

        void BindDeliveryCostList()
        {
            DataTable table = theDeliveryCost.GetList("").Tables[0];
            this.ddlDeliveryCost.DataTextField = "DeliveryCost";
            this.ddlDeliveryCost.DataValueField = "Id";
            this.ddlDeliveryCost.DataSource = table;
            this.ddlDeliveryCost.DataBind();
            this.ddlDeliveryCost.Items.Insert(0, new ExtAspNet.ListItem("请选择费用模板", "-1"));
        }
        void BuildForm()
        {
            if (!string.IsNullOrEmpty(Request.QueryString["id"]))
            {
                oDeliveryTemplate = theDeliveryTemplate.GetModel(int.Parse(Request.QueryString["id"]));
                if (oDeliveryTemplate != null)
                {
                    this.txtTemplateName.Text = oDeliveryTemplate.TemplateName;
                    if (this.ddlDeliveryCost.Items.FindByValue(oDeliveryTemplate.deliveryCostId.ToString()) != null)
                    {
                        this.ddlDeliveryCost.SelectedValue = oDeliveryTemplate.deliveryCostId.ToString();
                    }
                }
 
            }
        }
        protected void btnSaveRefresh_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(Request.QueryString["id"]))
            {
                oDeliveryTemplate = theDeliveryTemplate.GetModel(int.Parse(Request.QueryString["id"]));
                if (oDeliveryTemplate != null)
                {
                    oDeliveryTemplate.TemplateName = this.txtTemplateName.Text.Trim();
                    oDeliveryTemplate.deliveryCostId = int.Parse(this.ddlDeliveryCost.SelectedItem.Value);
                    theDeliveryTemplate.Update(oDeliveryTemplate);
                }
            }
            else
            {
                oDeliveryTemplate = new tbDeliveryTemplate();
                oDeliveryTemplate.TemplateName = this.txtTemplateName.Text.Trim();
                oDeliveryTemplate.deliveryCostId = int.Parse(this.ddlDeliveryCost.SelectedItem.Value);
                theDeliveryTemplate.Add(oDeliveryTemplate);
            }

            ExtAspNet.PageContext.RegisterStartupScript(ExtAspNet.ActiveWindow.GetHidePostBackReference());
        }
    }
}